package com.project.x;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;
import java.util.ArrayList;


/**
 * Created by Miko Arce on 2/13/2015.
 */
public class MessageHandler extends Activity{

    private Cursor cursor;
    private MessageEntry messageEntry;
    private ArrayList<MessageThread> messageThreadArrayList = new ArrayList<MessageThread>();

    public MessageHandler(){
        int log = Log.d("INIT","New MessageHandler initialized");
    }

    private MessageThread newMessageThread(Cursor cursor){
        MessageThread messageThread = new MessageThread();
        messageEntry = newMessageEntryVariables(cursor);
        messageThread = assignMessageThreadVariables(cursor, messageThread, messageEntry);

        return messageThread;
    }

    private MessageThread assignMessageThreadVariables(Cursor cursor, MessageThread messageThread,
                                                       MessageEntry messageEntry){
        messageThread.setPhoneNumber(cursor.getString(2));
        messageThread.setId(cursor.getInt(1));
        messageThread.setFirstName(cursor.getString(3));
        messageThread.setLastName("LastName");
        messageThread.addMessageEntryListElement(messageEntry);

        return messageThread;
    }

    private MessageEntry newMessageEntryVariables(Cursor cursor){
        messageEntry = new MessageEntry();

        messageEntry.setId(cursor.getInt(0));
        messageEntry.setDateTime(cursor.getString(4));
        messageEntry.setMessageContent(cursor.getString(12));
        messageEntry.setRead(cursor.getInt(7));
        messageEntry.setType(cursor.getInt(9));

        return messageEntry;
    }

    private int getExistingThread(String currentNumber, int id){
        if (messageThreadArrayList.isEmpty()) {
            int log = Log.d("MSG_EXIST_CHECK", "Message thread is empty!");
            return -1;
        }
        else {
            for (int i = 0; i < messageThreadArrayList.size(); i++) {
                if (messageThreadArrayList.get(i).getId() == id){
                    int log = Log.d("MSG_EXIST_CHECK", "Updating thread with ID: "+ id +
                            ", num: " + currentNumber);
                    return i;
                }
            }
            return -1;
        }
    }

    public ArrayList<MessageThread> extractAllDbMessages(Context context){
        cursor = context.getContentResolver().query(Uri.parse("content://sms/"), null, null, null,
                "ADDRESS, DATE desc");
        cursor.moveToFirst();

        String currentPhoneNum;
        int currentThreadID;
        int focusedIndex;
        do{
            currentPhoneNum = cursor.getString(2);
            currentThreadID = cursor.getInt(1);

            int log = Log.v("MSG_INFO", cursor.getString(1) + " : " + currentPhoneNum + " = " + cursor.getString(12)
                    + " date : " + cursor.getString(4));
                    focusedIndex = getExistingThread(currentPhoneNum, currentThreadID);

            if (focusedIndex == -1){ //no existing thread
                log = Log.v("NumEntry","Did not see duplicate entry for ID : " +
                        currentThreadID + " : " + currentPhoneNum);
                //Initialize new messageThread and messageEntry
                messageThreadArrayList.add(this.newMessageThread(cursor));
            }
            else {
                log = Log.v("NumEntry", "Number already exists in list for ID : " +
                        currentThreadID + " : " + currentPhoneNum);
                //Update messageThread content using index focusedIndex;
                messageThreadArrayList.get(focusedIndex).getMessageEntryList().
                        add(newMessageEntryVariables(cursor));
            }
        }while(cursor.moveToNext());

        return messageThreadArrayList;
    }
}

/*
            (inside do-while) for reference:
            String msgData = "Start:\n";

            for(int i=0; i<cursor.getColumnCount(); i++){
                msgData += " " + cursor.getColumnName(i) + ":" + cursor.getString(i);

                * Important Indexes
                * message ID: 0
                * thread ID: 1
                * address(phone number): 2
                * person (always null?): 3
                * date: 4
                * read: 7
                * type: 9
                * body: 12
                *
                *
            }*/